package cn.chuxing.config.security;

import cn.chuxing.dao.model.PlUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public final class JwtUserFactory {

    private JwtUserFactory() {
    }

    public static JwtUser create(PlUser user) {
        return new JwtUser(
                String.valueOf(user.getUserId()),
                user.getUsername(),
                user.getPassword(),
                mapToGrantedAuthorities(Collections.singletonList(user.getUsername()))
        );
    }

    private static List<GrantedAuthority> mapToGrantedAuthorities(List<String> authorities) {
    	if(authorities==null){
    		return null;
    	}
        return authorities.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }
}

